
package com.probiz.estore.content.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.content.AdPositionType;
import com.probiz.estore.common.model.content.Advertisement;
import com.probiz.estore.common.model.content.AdvertisementPublish;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.content.dao.AdvertisementDao;
import com.probiz.estore.content.model.AdvertisementFullyModel;
import com.probiz.estore.content.service.AdPositionTypeManager;
import com.probiz.estore.content.service.ProductAdvertisementManager;
import com.probiz.estore.core.service.impl.GenericManagerImpl;

public class AdvertisementManagerImpl extends GenericManagerImpl<Advertisement> implements com.probiz.estore.content.service.AdvertisementManager {
	
	private AdvertisementDao advertisementDao=null;
	private ProductCategoryManager  productCategoryManager=null;
	private ProductAdvertisementManager	productAdvertisementManager	= null;
	private AdPositionTypeManager		adPositionTypeManager	= null;
	
	@Override
	protected void initManager() {
		this.dao=advertisementDao;
		
	}

	@Override
	protected void preDelete(Advertisement entity) {
		
	}

	@Override
	protected void preSave(Advertisement entity) {
		
	}

	public void setAdvertisementDao(AdvertisementDao advertisementDao) {
		this.advertisementDao = advertisementDao;
	}
	
	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}
	
	public void setAdPositionTypeManager(AdPositionTypeManager mgr) {
		this.adPositionTypeManager = mgr;
	}

	public void setProductAdvertisementManager(ProductAdvertisementManager mgr) {
		this.productAdvertisementManager = mgr;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.service.AdvertisementManager#saveFully(com.probiz.estore.content.model.AdvertisementFullyModel)
	 */
	public Map<String, Object> saveFully(AdvertisementFullyModel model){
		Map<String, Object> returnMsgs = new HashMap<String, Object>();
		Advertisement advertisement = model.getAdvertisement();
		//start:设置小时
		Calendar c = Calendar.getInstance();
		if (null != advertisement.getStartPublishTime()) {
			c.setTime(advertisement.getStartPublishTime());
			c.set(Calendar.HOUR_OF_DAY, advertisement.getStartHour());
			advertisement.setStartPublishTime(c.getTime());
		}
		if (null != advertisement.getEndPublishTime()) {
			c.setTime(advertisement.getEndPublishTime());
			c.set(Calendar.HOUR_OF_DAY, advertisement.getEndHour());
			advertisement.setEndPublishTime(c.getTime());
		}
		//end:设置小时
		//设置状态
		setStatus(advertisement);
		//第一次保存
		save(advertisement);
		productAdvertisementManager.saveProductAdvertisementsFromArrays(advertisement,model.getCategoryIds(),model.getIsSubIncludeds());
		save(advertisement);
		return returnMsgs;
	}
	
	
	 /**
     * 设置广告状态
     *
     * @param content 文章
     */
    private void setStatus(Advertisement advertisement) {
        if (advertisement.getIsShow() == null || advertisement.getIsShow().shortValue() == Constants.FLAG_FALSE.shortValue()) {
        	advertisement.setStatus(Content.STATUS_INACTIVE);
        } else {
            Calendar calendar = Calendar.getInstance();
            //稍稍延长一点时间
            calendar.add(Calendar.SECOND, 10);
            Date now = calendar.getTime();
            if (now.getTime() <= advertisement.getStartPublishTime().getTime()) {
            	advertisement.setStatus(Content.STATUS_WAITING_FOR_PUBLISH);
            } else if ((now.getTime() > advertisement.getStartPublishTime().getTime()) && (advertisement.getEndPublishTime() == null || now.getTime() < advertisement.getEndPublishTime().getTime())) {
            	advertisement.setStatus(Content.STATUS_PUBLISHED);
            } else {
            	advertisement.setStatus(Content.STATUS_INACTIVE);
            }
        }
    }


	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.service.AdvertisementManager#getAdvertisementsForFront(java.lang.Integer, java.lang.Integer)
	 */
	public List<Advertisement> getAdvertisementsForFront(Integer adPositionTypeId, Integer categoryId) {
		//获得所有父目录id（不含根目录）
		List<Integer> subCategoryIdList=this.productCategoryManager.getAllParentCategoryId(categoryId);
		StringBuffer categoryIds = new StringBuffer(50);
		if (!categoryId.equals(Constants.ROOT_CATEGORY)) {
			//如果是根目录的话不需要考虑子目录
			for (Integer subCategoryId : subCategoryIdList) {
				categoryIds.append(subCategoryId).append(",");
			}
		}
		categoryIds.append(categoryId);
		
		List<Advertisement> results=this.advertisementDao.getAdvertisementsForFront(adPositionTypeId, categoryId, categoryIds.toString());
		return results;
				
		
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.service.AdvertisementManager#getAdvertisementPublishForFront(java.lang.String, java.lang.Integer)
	 */
	public AdvertisementPublish getAdvertisementPublishForFront(String positionCode,Integer categoryId){
		AdPositionType adPositionType = adPositionTypeManager.getAdPositionTypeByCode(positionCode);
		if( null == adPositionType){
			return null;
		}
		if (null == categoryId) {
			categoryId = Constants.ROOT_CATEGORY; //赋予一个默认值：根目录
		}
		
		List<Advertisement> advertisementList = this.getAdvertisementsForFront(adPositionType.getAdPositionTypeId(), categoryId);
		// 随机的
		if (advertisementList.size() > 1 && AdPositionType.DISPLAY_TYPE_RANDOM.equals(adPositionType.getDisplayType())) {
			Advertisement advertisement = advertisementList.get(new Random().nextInt(advertisementList.size()));
			advertisementList.clear();
			advertisementList.add(advertisement);
		}
		if(advertisementList.size() > 0 ){
			AdvertisementPublish advertisementPublish = new AdvertisementPublish();
			advertisementPublish.setAdPositionType(adPositionType);
			advertisementPublish.setAdvertisements(advertisementList);
			return advertisementPublish;
		}else{
			return null;
		}
		
	}

	
}
